package com.example.demo.widget.chart.flags

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.View
import com.example.demo.R
import com.example.demo.widget.chart.utils.DrawTextUtils
import com.example.demo.widget.chart.ChartItem
import kotlin.math.roundToInt

/**
 * 报表Flags View
 *
 * @author wangheng
 */
class ChartFlagsView : View {
    constructor(context: Context?) : super(context){
        initView(context,null)
    }
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){
        initView(context,attrs)
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        initView(context,attrs)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes){
        initView(context,attrs)
    }

    private var mData: ArrayList<ChartItem>? = null

    private var mFlagTextSize = 12f
    private var mFlagTextColor = Color.BLACK
    private var mFlagCircleRadius = 5f
    private var mFlagCircleTextPadding = 5f
    private var mFlagPadding = 10f

    private var mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var mCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private fun initView(context: Context?, attrs: AttributeSet?){
        if(context != null) {
            val ta = context.obtainStyledAttributes(attrs, R.styleable.ChartFlagsView)
            // app:chart_flags_text_size 颜色标识文字大小
            mFlagTextSize = ta.getDimension(R.styleable.ChartFlagsView_chart_flags_text_size, mFlagTextSize)
            // app:chart_flags_text_color 颜色标识文字颜色
            mFlagTextColor = ta.getColor(R.styleable.ChartFlagsView_chart_flags_text_color, mFlagTextColor)
            // app:chart_flags_circle_radius 颜色标识圆形半径
            mFlagCircleRadius = ta.getDimension(
                R.styleable.ChartFlagsView_chart_flags_circle_radius,
                mFlagCircleRadius)
            // app:chart_flags_circle_text_padding 颜色标识文字和圆形之间的距离
            mFlagCircleTextPadding = ta.getDimension(
                R.styleable.ChartFlagsView_chart_flags_circle_text_padding,
                mFlagCircleTextPadding)
            // app:chart_flags_padding 两个标识之间的距离
            mFlagPadding = ta.getDimension(R.styleable.ChartFlagsView_chart_flags_padding, mFlagPadding)
            ta.recycle()
        }

        mCirclePaint.style = Paint.Style.FILL
        mTextPaint.textSize = mFlagTextSize
        mTextPaint.color = mFlagTextColor
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val heightMode = MeasureSpec.EXACTLY
        val maxSize = getTextCircleMaxSize()
        val heightSpec = MeasureSpec.makeMeasureSpec((maxSize + paddingTop + paddingBottom).roundToInt(),heightMode)
        super.onMeasure(widthMeasureSpec, heightSpec)
    }

    private fun getTextCircleMaxSize(): Float{
        return if(mFlagTextSize > mFlagCircleRadius * 2){
            mFlagTextSize
        }else{
            mFlagCircleRadius * 2
        }
    }

    fun setData(list: ArrayList<ChartItem>){
        mData = list
        postInvalidate()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val list = mData ?: return
        val size = list.size
        if(canvas == null || list.size == 0){
            return
        }

        val widthSize = width - paddingLeft - paddingRight

        // 圆点的个数 x 每个圆点的直径  == 所有圆点加在一起的大小
        val circleTotalSize = mFlagCircleRadius * 2 * size
        // 两个Flag之间的间距，总共有长度减一个
        val paddingTotalSize = (size - 1) * mFlagPadding
        // 文字和小圆点之间的距离的总长度(个数乘以大小)
        val chartFlagPaddingTotalSize = size * mFlagCircleTextPadding
        // 所有文字的长度的和
        val textTotalSize = list.fold(0f){ result,item ->
            result + mTextPaint.measureText(item.title)
        }
        val drawSize = circleTotalSize + paddingTotalSize + chartFlagPaddingTotalSize + textTotalSize
        val delta = (widthSize - drawSize) / 2f
        // 绘制开始位置
        var startLeft = delta + paddingLeft
        val startTop = paddingTop.toFloat()
        val maxHeight = getTextCircleMaxSize()
        list.forEach {
            if(!it.onlyDrawColor){
                mCirclePaint.color = it.color

                val centerX = startLeft + mFlagCircleRadius
                val centerY = startTop + mFlagCircleRadius + (maxHeight - mFlagCircleRadius * 2) / 2f
                canvas.drawCircle(centerX,centerY,mFlagCircleRadius,mCirclePaint)

                startLeft += 2 * mFlagCircleRadius + mFlagCircleTextPadding

                val left = startLeft
                val top = startTop
                val right = startLeft + mTextPaint.measureText(it.title)
                val bottom = top + maxHeight

                DrawTextUtils.drawTextOnRectCenter(
                    canvas, mTextPaint, it.title,
                    left, top, right, bottom
                )
                startLeft = right +  mFlagPadding
            }
        }
    }
}